//获取应用实例
var app = getApp()
Page({
  data: {
    user: {},
    sourceUrl: 'https://eis.natappvip.cc/ITSupport',
    topics:{},
    reply:{},
    unApproves: {},
    approved:{},

    inviteNewsNum: 0,
    replyNewsNum: 0,
    unApprovesNewsNum:0,
    approvedNewsNum:0,

    replyIconUrl: "../../image/icon-arrowdown.png",
    inviteIconUrl: "../../image/icon-arrowdown.png",
    unApproveIconUrl: "../../image/icon-arrowdown.png",
    approvedIconUrl: "../../image/icon-arrowdown.png",

    isReplyUnReaded:false,
    isInviteUnReaded:false,
    replyNewsUnReadNum:0,
    inviteNewsUnReadNum:0,

    isReplyUnfold:false,
    isTopicsUnfold: false,
    isUnApprovedUnfold:false,
    isApprovedUnfold: false
  },

  onLoad: function () {
    this.setData({
      user: app.globalData.user
    })
    this.getReplyNews(),
    this.getInvitaNews(),
    this.getUnApproveNews(),
    this.getApprovedNews()
  },

  onShow: function () {
    this.setData({
      topics: {},
      reply: {},
      unApproves: {},
      approved: {},

      inviteNewsNum: 0,
      replyNewsNum: 0,
      unApprovesNewsNum: 0,
      approvedNewsNum: 0,

      replyIconUrl: "../../image/icon-arrowdown.png",
      inviteIconUrl: "../../image/icon-arrowdown.png",
      unApproveIconUrl: "../../image/icon-arrowdown.png",
      approvedIconUrl: "../../image/icon-arrowdown.png",

      isReplyUnReaded: false,
      isInviteUnReaded: false,
      replyNewsUnReadNum: 0,
      inviteNewsUnReadNum: 0,

      isReplyUnfold: false,
      isTopicsUnfold: false,
      isUnApprovedUnfold: false,
      isApprovedUnfold: false
    })
    this.onLoad();
  },

  showReplyNews:function(){
    if(this.data.replyIconUrl == "../../image/icon-arrowdown.png"){
      var that = this
      wx.request({
        url: 'https://eis.natappvip.cc/ITSupport/reply_updateNewsByTopicUser.action',
        data: {
          'user.userId': this.data.user.userId,
        },
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        success: function (res) {
          let jsonresult = JSON.parse(res.data)
          console.log(jsonresult.result)
          if (jsonresult.result == "error") {
            // that.setData({
            //   message: "页面空空如也~"
            // })
          } else {
            that.setData({
              isReplyUnReaded: false,
              replyIconUrl: "../../image/icon-arrowdown_next.png",
              isReplyUnfold: true
            })
            console.log("success")
          }
        },
        fail: function (res) {
          console.log("failed:" & res)
        }
      })
    }else{
      this.setData({
        replyIconUrl: "../../image/icon-arrowdown.png",
        isReplyUnfold: false
      })
    }
  },

  showInviteNews: function () {
    if (this.data.inviteIconUrl == "../../image/icon-arrowdown.png") {
      var that = this
      wx.request({
        url: 'https://eis.natappvip.cc/ITSupport/topic_updateNewsByToUser.action',
        data: {
          'user.userId': this.data.user.userId,
        },
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        success: function (res) {
          let jsonresult = JSON.parse(res.data)
          console.log(jsonresult.result)
          if (jsonresult.result == "error") {
            // that.setData({
            //   message: "页面空空如也~"
            // })
          } else {
            that.setData({
              isInviteUnReaded: false,
              inviteIconUrl: "../../image/icon-arrowdown_next.png",
              isTopicsUnfold: true
            })
            console.log("success")
          }
        },
        fail: function (res) {
          console.log("failed:" & res)
        }
      })
    } else {
      this.setData({
        inviteIconUrl: "../../image/icon-arrowdown.png",
        isTopicsUnfold: false
      })
    }
  },

  showUnApproveNews: function () {
    if (this.data.unApproveIconUrl == "../../image/icon-arrowdown.png") {
          this.setData({
            unApproveIconUrl: "../../image/icon-arrowdown_next.png",
            isUnApprovedUnfold: true
        })
    } else {
      this.setData({
        unApproveIconUrl: "../../image/icon-arrowdown.png",
        isUnApprovedUnfold: false
      })
    }
  },

  showApprovedNews: function () {
    if (this.data.approvedIconUrl == "../../image/icon-arrowdown.png") {
      this.setData({
        approvedIconUrl: "../../image/icon-arrowdown_next.png",
        isApprovedUnfold: true
      })
    } else {
      this.setData({
        approvedIconUrl: "../../image/icon-arrowdown.png",
        isApprovedUnfold: false
      })
    }
  },

  getReplyNews:function(){
    var that = this
    wx.request({
      url: 'https://eis.natappvip.cc/ITSupport/reply_findNewsByTopicUser.action',
      data: {
        'user.userId': this.data.user.userId,
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        let jsonresult = JSON.parse(res.data)
        console.log(jsonresult.result)
        if (jsonresult.result == "error") {
          // that.setData({
          //   message: "页面空空如也~"
          // })
        } else {
          that.setData({
            reply: jsonresult.reply,
            replyNewsNum: jsonresult.reply.length
          })
          for (var i = 0; i < that.data.replyNewsNum; i++) {
            if (that.data.reply[i].isReaded == 0) {
              that.setData({
                replyNewsUnReadNum: that.data.replyNewsUnReadNum+1,
              })
            }
          }
          if (that.data.replyNewsUnReadNum > 0){
            that.setData({
              isReplyUnReaded: true,
            })
          }
          console.log("success")
        }
      },
      fail: function (res) {
        console.log("failed:" & res)
      }
    })
  },

  getInvitaNews: function () {
    var that = this
    wx.request({
      url: 'https://eis.natappvip.cc/ITSupport/topic_getNewsByToUser.action',
      data: {
        'user.userId': this.data.user.userId,
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        let jsonresult = JSON.parse(res.data)
        console.log(jsonresult.result)
        if (jsonresult.result == "error") {
          // that.setData({
          //   message: "页面空空如也~"
          // })
        } else {
          that.setData({
            topics: jsonresult.topics,
            inviteNewsNum: jsonresult.topics.length
          })
          for (var i = 0; i < that.data.inviteNewsNum; i++) {
            if (that.data.topics[i].isReaded == 0) {
              that.setData({
                inviteNewsUnReadNum: that.data.inviteNewsUnReadNum + 1,
              })
            }
          }
          if (that.data.inviteNewsUnReadNum > 0) {
            that.setData({
              isInviteUnReaded: true,
            })
          }
          console.log("success")
        }
      },
      fail: function (res) {
        console.log("failed:" & res)
      }
    })
  },

  getUnApproveNews: function () {
    var that = this
    wx.request({
      url: 'https://eis.natappvip.cc/ITSupport/approve_getAllUnapprovedByApprover.action',
      data: {
        'user.userId': this.data.user.userId,
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        let jsonresult = JSON.parse(res.data)
        console.log(jsonresult.result)
        if (jsonresult.result == "error") {
          // that.setData({
          //   message: "页面空空如也~"
          // })
        } else {
          that.setData({
            unApproves: jsonresult.approves,
            unApprovesNewsNum: jsonresult.approves.length
          })
          console.log("success")
        }
      },
      fail: function (res) {
        console.log("failed:" & res)
      }
    })
  },

  getApprovedNews: function () {
    var that = this
    wx.request({
      url: 'https://eis.natappvip.cc/ITSupport/approve_getAllApprovedByApprover.action',
      data: {
        'user.userId': this.data.user.userId,
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        let jsonresult = JSON.parse(res.data)
        console.log(jsonresult.result)
        if (jsonresult.result == "error") {
          // that.setData({
          //   message: "页面空空如也~"
          // })
        } else {
          that.setData({
            approved: jsonresult.approves,
            approvedNewsNum: jsonresult.approves.length
          })
          console.log("success")
        }
      },
      fail: function (res) {
        console.log("failed:" & res)
      }
    })
  },

  topicDetail: function(e) {
    var $topicId = e.currentTarget.dataset.topicId
    wx.navigateTo({
      url: '../topic/topic?topicId=' + $topicId
    })
  },

  approveDetail: function (e) {
    var $approveId = e.currentTarget.dataset.approveId
    wx.navigateTo({
      url: '../approve/approve?approveId=' + $approveId
    })
  }

})
